//
//  titleView.m
//  集成ReactNative
//
//  Created by Admin on 2017/3/6.
//  Copyright © 2017年 Admin. All rights reserved.
//

#define kScrollH 2

#import "titleView.h"

@interface titleView ()
@property (nonatomic,strong) NSArray <NSString *> *titles;
@property (nonatomic,strong) NSMutableArray <UILabel *> *titleLabels;
@property (nonatomic,strong) UIScrollView *scrollView;
@property (nonatomic,strong) UIView *scrollLine;
@property (nonatomic,assign) NSInteger currentIndex;
@end

@implementation titleView

#pragma mark - 懒加载
-(UIScrollView *)scrollView {
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc]init];
        _scrollView.showsHorizontalScrollIndicator = false;
        _scrollView.scrollsToTop = false;
        _scrollView.bounces = false;
        _scrollView.frame = self.bounds;
    }
    return _scrollView;
}

#pragma mark - 懒加载
-(NSMutableArray *)titleLabels {
    if (_titleLabels == nil) {
        _titleLabels = [NSMutableArray array];
    }
    return _titleLabels;
}

#pragma mark - 懒加载
-(UIView *)scrollLine {
    if (_scrollLine == nil) {
        _scrollLine = [[UIView alloc]init];
        _scrollLine.backgroundColor = [UIColor orangeColor];
    }
    return _scrollLine;
}

- (instancetype)initWithFrame:(CGRect)frame titles:(NSArray *)titles {
    self = [super initWithFrame:frame];
    if (self) {
        self.titles = titles;
        [self setupUI];
        [self setupTitleLabel];
        [self setupScrollLine];
    }
    return self;
}

- (void)setupUI {
    [self addSubview:self.scrollView];
}

- (void)setupTitleLabel {
    CGFloat labelW = self.frame.size.width / self.titles.count;
    CGFloat labelH = self.frame.size.height - kScrollH;
    CGFloat labelY = 0;
    for (int i = 0; i<self.titles.count; i++) {
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.text = self.titles[i];
        titleLabel.tag = i;
        titleLabel.textColor = [UIColor darkGrayColor];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.font = [UIFont systemFontOfSize:15];
        CGFloat labelX = labelW * i;
        titleLabel.frame = CGRectMake(labelX, labelY, labelW, labelH);
        [self.scrollView addSubview:titleLabel];
        [self.titleLabels addObject:titleLabel];
        titleLabel.userInteractionEnabled = true;
        // 5.给Label添加手势
        UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(titleLabelClick:)];
        [titleLabel addGestureRecognizer:tapGes];
    }
}

- (void)setupScrollLine {
    UIView *bottomLine = [[UIView alloc] init];
    bottomLine.backgroundColor = [UIColor lightGrayColor];
    CGFloat lineH = 0.5;
    bottomLine.frame = CGRectMake(0, self.frame.size.height - lineH, self.frame.size.width, lineH);
    [self addSubview:bottomLine];
    // 2.1.获取第一个Label
    UILabel *firstLabel = self.titleLabels.firstObject;
    firstLabel.textColor = [UIColor redColor];
    [self.scrollView addSubview:self.scrollLine];
    // 2.2.设置scrollLine的属性
    self.scrollLine.frame = CGRectMake(firstLabel.frame.origin.x, firstLabel.frame.size.height - 2, firstLabel.frame.size.width, 2);
}

- (void)titleLabelClick:(UITapGestureRecognizer *)tapGes {
    NSLog(@"点击了label");
    // 0.获取当前Label
    UILabel *currentLabel = (UILabel *)tapGes.view;
    // 1.如果是重复点击同一个Title,那么直接返回
    if (currentLabel.tag == self.currentIndex) return;
    // 2.获取之前的Label
    UILabel *oldLabel = self.titleLabels[self.currentIndex];
    // 3.切换文字的颜色
    currentLabel.textColor = [UIColor redColor];
    oldLabel.textColor = [UIColor darkGrayColor];
    // 4.保存最新Label的下标值
    self.currentIndex = currentLabel.tag;
    // 5.滚动条位置发生改变
    CGFloat scrollLineX = self.currentIndex * self.scrollLine.frame.size.width;
    [UIView animateWithDuration:0.15 animations:^{
        CGRect frame = self.scrollLine.frame;
        frame.origin.x = scrollLineX;
        self.scrollLine.frame = frame;
    }];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(titleViewSelectedIndex:)]) {
        [self.delegate titleViewSelectedIndex:self.currentIndex];
    }
    
}
- (void)setTitleWithProgress:(CGFloat)progress sourceIndex:(NSInteger)sourceIndex targetIndex:(NSInteger)targetIndex {
    // 1.取出sourceLabel/targetLabel
//    NSLog(@"sourceIndex..%ld,targetIndex..%ld",(long)sourceIndex,(long)targetIndex);
    UILabel *sourceLabel = self.titleLabels[sourceIndex];
    UILabel *targetLabel = self.titleLabels[targetIndex];
    // 2.处理滑块的逻辑
    CGFloat moveTotalX = targetLabel.frame.origin.x - sourceLabel.frame.origin.x;
    CGFloat moveX = moveTotalX * progress;
    CGRect frame = self.scrollLine.frame;
    frame.origin.x = sourceLabel.frame.origin.x + moveX;
    self.scrollLine.frame = frame;
    NSLog(@"progress..%f",255 *progress);
//    Runtime is not ready for debugging. Make sure Packager server is running.
//        
//        RCTFatal
//        -[RCTBatchedBridge _processResponse:error:]
//        __69-[RCTBatchedBridge _actuallyInvokeAndProcessModule:method:arguments:]_block_invoke
//        __58-[RCTWebSocketExecutor _executeJSCall:arguments:callback:]_block_invoke
//        __44-[RCTWebSocketExecutor sendMessage:onReply:]_block_invoke
//        _dispatch_call_block_and_release
//        _dispatch_client_callout
//        _dispatch_queue_serial_drain
//        _dispatch_queue_invoke
//        _dispatch_queue_override_invoke
//        _dispatch_root_queue_drain
//        _dispatch_worker_thread3
//        _pthread_wqthread
//        start_wqthread
    // 3.颜色的渐变(复杂)
    // 3.1.取出变化的范围
    CGFloat kSelectColorR = 1 - (1-0.7) * progress;
    CGFloat kSelectColorG = 0 - (0 - 0.7) * progress;
    CGFloat kSelectColorB = 0 - (0 - 0.7) * progress;
//
    CGFloat kNormalColorR = 0.7 + (1-0.7) * progress;
    CGFloat kNormalColorG = 0.7 + (0 - 0.7) * progress;
    CGFloat kNormalColorB = 0.7 + (0 - 0.7) * progress;
    NSLog(@"R..%f   G..%f   B..%f",kSelectColorR,kSelectColorG,kSelectColorB);
    NSLog(@"R..%f G..%f B..%f",kNormalColorR,kNormalColorG,kNormalColorB);
    // 3.2.变化sourceLabel
    sourceLabel.textColor = [UIColor colorWithRed:kSelectColorR green:kSelectColorG blue:kSelectColorB alpha:1.0];
//
//    // 3.2.变化targetLabel
    targetLabel.textColor = [UIColor colorWithRed:kNormalColorR green:kNormalColorG blue:kNormalColorB alpha:1.0];
    
    // 4.记录最新的index
    self.currentIndex = targetIndex;
}
@end
